package cn.sfrog.project.biz.admin.controller;

import cn.sfrog.common.annotation.Log;
import cn.sfrog.common.annotation.RepeatSubmit;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.AjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.enums.BusinessType;
import cn.sfrog.common.utils.poi.ExcelUtil;
import cn.sfrog.project.biz.admin.domain.vo.*;
import cn.sfrog.project.biz.admin.enums.BizOrderStatusEnum;
import cn.sfrog.project.biz.admin.service.IBizServiceOrderService;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.domain.vo.biz.BizDispatchUserQueryVO;
import cn.sfrog.system.domain.vo.biz.BizUserResultVO;
import cn.sfrog.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "管理后台-服务订单相关")
@RestController
@RequestMapping("/biz/serviceOrder")
public class BizServiceOrderController extends BaseController {
    @Resource
    private IBizServiceOrderService bizServiceOrderService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询服务订单列表
     */
    @ApiOperation("查询列表")
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizServiceOrderQueryVO queryVO) {
        startPage();
        List<BizServiceOrderVO> list = bizServiceOrderService.selectBizServiceOrderList(queryVO);
        return getDataTable(list);
    }



    /**
     * 导出服务订单列表
     */
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:export')")
    @Log(title = "服务订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizServiceOrderQueryVO query) {
        List<BizServiceOrderVO> list = bizServiceOrderService.selectBizServiceOrderList(query);
        ExcelUtil<BizServiceOrderVO> util = new ExcelUtil<BizServiceOrderVO>(BizServiceOrderVO.class);
        util.exportExcel(response, list, "服务订单数据");
    }

    /**
     * 获取服务订单详细信息
     */
    @ApiOperation("获取详情")
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizServiceOrderService.selectBizServiceOrderById(id));
    }

    /**
     * 新增服务订单
     */
    @ApiOperation("新增")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:add')")
    @Log(title = "服务订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizServiceOrderFormVO form) {
        return toAjax(bizServiceOrderService.insertBizServiceOrder(form));
    }

    /**
     * 修改服务订单
     */
    @ApiOperation("修改")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:edit')")
    @Log(title = "服务订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizServiceOrderFormVO form) {
        return toAjax(bizServiceOrderService.updateBizServiceOrder(form));
    }

    /**
     * 删除服务订单
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:remove')")
    @Log(title = "服务订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizServiceOrderService.deleteBizServiceOrderByIds(ids));
    }

    /**
     * 更改状态服务订单
     */
    @ApiOperation("更改状态")
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:changeStatus')")
    @Log(title = "服务订单", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody @Valid BizStatusChangeVO vo) {
        return AjaxResult.success(bizServiceOrderService.changeStatus(vo));
    }

    @ApiOperation("取消订单")
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:cancelOrder')")
    @Log(title = "服务订单", businessType = BusinessType.UPDATE)
    @PostMapping("/cancelOrder/{id}")
    public AjaxResult cancelOrder(@PathVariable("id") Long id){
        return toAjax(bizServiceOrderService.cancel(id));
    }


    /**
     * 查询服务订单列表
     */
    @ApiOperation("查询待派单列表")
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:dispatchList')")
    @GetMapping("/dispatchList")
    public TableDataInfo dispatchList(BizServiceOrderQueryVO queryVO) {
        queryVO.setOrderStatus(BizOrderStatusEnum.PENDING_SERVICE.getCode());
        startPage();
        List<BizServiceOrderVO> list = bizServiceOrderService.selectBizServiceOrderDispatchList(queryVO);
        return getDataTable(list);
    }

    /**
     * 派单
     * @param formVO
     * @return
     */
    @ApiOperation("派单")
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:dispatch')")
    @PostMapping("/dispatch")
    public AjaxResult dispatch(@RequestBody @Valid BizServiceOrderDispatchFormVO formVO){
        return toAjax(bizServiceOrderService.dispatch(formVO));
    }

    @ApiOperation("查询可派单人员")
    @GetMapping("/getDispatchUserList")
    public AjaxResult getDispatchUserList(BizDispatchUserQueryVO queryVO){
        List<BizUserResultVO> userList =  sysUserService.selectDispatchUserList(queryVO);
        return AjaxResult.success(userList);
    }

    @ApiOperation("客服代下单")
    @PostMapping("/helpCustomerOrder")
    @PreAuthorize("@ss.hasPermi('biz:serviceOrder:helpCustomerOrder')")
    public AjaxResult helpCustomerOrder(@RequestBody @Valid BizHelpCustomerOrderFormVO form){
        return AjaxResult.success(bizServiceOrderService.createHelpCustomerOrder(form));
    }
}
